<?php

namespace app\admin\controller;

use app\BaseController;
use app\model\ConsultOfflineFactorScoreModel;
use app\model\ConsultOfflineResultModel;
use app\model\ConsultOnlineFactorScoreModel;
use app\model\ConsultOnlineResultModel;
use app\model\FollowUserModel;
use app\model\InterfereBehaviorModel;
use app\model\InterfereUserModel;
use app\model\ProxyEvaluationResultModel;
use app\model\ProxyFactorScoreModel;
use app\model\RatingScaleFactorWarningModel;
use app\model\RatingScaleModel;
use app\model\SelfEvaluationResultModel;
use app\model\SelfFactorScoreModel;
use app\model\TaskFactorScoreModel;
use app\model\TaskModel;
use app\model\TaskUserModel;
use app\model\TaskUserResultModel;
use app\model\not_del\UserModel;
use app\model\WarningReportRecordModel;
use think\App;

class AdvancedFilter extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 得分选择器
     */
    public function ScoreSelector(): bool|string
    {
        try {
            return success($this->ScoreSelectorConfig());
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 得分选择器配置
     * @param int $key
     * @return array|array[]
     */
    public function ScoreSelectorConfig(int $key = 0): array
    {
        $data = [
            [
                'id' => 1,
                'name' => '无得分',
                'value' => 0,
                'calculation' => '='
            ],
            [
                'id' => 2,
                'name' => '0分-10分',
                'value' => [0, 10],
                'calculation' => 'between'
            ],
            [
                'id' => 3,
                'name' => '11分-20分',
                'value' => [11, 20],
                'calculation' => 'between'
            ],
            [
                'id' => 4,
                'name' => '21分-30分',
                'value' => [21, 30],
                'calculation' => 'between'
            ],
            [
                'id' => 5,
                'name' => '31分-40分',
                'value' => [31, 40],
                'calculation' => 'between'
            ],
            [
                'id' => 6,
                'name' => '41分-50分',
                'value' => [41, 50],
                'calculation' => 'between'
            ],
            [
                'id' => 7,
                'name' => '51分-60分',
                'value' => [51, 60],
                'calculation' => 'between'
            ],
            [
                'id' => 8,
                'name' => '61分-70分',
                'value' => [61, 70],
                'calculation' => 'between'
            ],
            [
                'id' => 9,
                'name' => '71分-80分',
                'value' => [71, 80],
                'calculation' => 'between'
            ],
            [
                'id' => 10,
                'name' => '81分-90分',
                'value' => [81, 90],
                'calculation' => 'between'
            ],
            [
                'id' => 11,
                'name' => '91分-100分',
                'value' => [91, 100],
                'calculation' => 'between'
            ],
            [
                'id' => 12,
                'name' => '100分以上',
                'value' => 100,
                'calculation' => '>'
            ]
        ];
        return $key ? $data[$key - 1] : $data;
    }

    /**
     * 预警等级选择器
     * @return bool|string
     */
    public function WarningSelector(): bool|string
    {
        try {
            $id = $this->request->param("id");
            $RatingScaleModel = new RatingScaleModel();
            $RatingScaleFactorWarningModel = new RatingScaleFactorWarningModel();
            $info = $RatingScaleModel->find($id);
            if (!$info) throw new \Exception('参数错误！');
            $mark_count = $RatingScaleFactorWarningModel->where([
                ['rating_scale_id', '=', $info['id']],
                ['mode_id', '=', $info['mode_id']]
            ])->order('mark_count', 'desc')->value('mark_count');
            return success($mark_count);
        } catch (\Exception $e) {
            return error($e->getMessage(), 0, [
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 关注人群高级筛选
     * @return bool|string
     */
    public function FollowUser(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['department_id'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['uid', '=', -1];
                }
            }
            if (!empty($param['level'])) {
                $where[] = ['level', 'in', $param['level']];
            }
            if (!empty($param['channel'])) {
                $where[] = ['channel', 'in', $param['channel']];
            }
            if (isset($param['is_up']) && strlen($param['is_up']) > 0) {
                if ($param['is_up'] == 1) {
                    $where[] = ['up_num', '>', 0];
                } else {
                    $where[] = ['up_num', '=', 0];
                }
            }
            $ResultWhere = [];
            if (!empty($param['warning'])){
                $warning_id = [];
                $RatingScaleFactorWarningModel = new RatingScaleFactorWarningModel();
                foreach ($param['warning'] as $item) {
                    if (empty($item['mark_count'])) continue;
                    $factor_warning_id = $RatingScaleFactorWarningModel->where([
                        ['rating_scale_id','=',$item['rating_scale_id']],
                        ['mark_count','in',$item['mark_count']]
                    ])->column('id');
                    if (count($factor_warning_id)) {
                        $warning_id = array_merge($warning_id,$factor_warning_id);
                    }
                }
                if (count($warning_id)){
                    $ResultWhere[] = ['warning_id','in',$warning_id];
                }else{
                    $ResultWhere[] = ['id','=',-1];
                }
            }
            if (!empty($param['factor_id'])) {
                $factor_id = [];
                foreach ($param['factor_id'] as $item) {
                    if (count($item)) {
                        $factor_id = array_merge($factor_id,$item);
                    }
                }
                if (count($factor_id)) {
                    $ResultWhere[] = ['factor_id', 'in', $factor_id];
                } else {
                    $ResultWhere[] = ['id', '=', -1];
                }
            }
            $result_ids = [];
            if (count($ResultWhere) > 0) {
                $ResultWhere[] = ['is_warning','=',1];
                $TaskFactorScoreModel = new TaskFactorScoreModel();
                $task_result_id = $TaskFactorScoreModel->where($ResultWhere)->column('result_id');
                $SelfFactorScoreModel = new SelfFactorScoreModel();
                $self_result_id = $SelfFactorScoreModel->where($ResultWhere)->column('result_id');
                $ProxyFactorScoreModel = new ProxyFactorScoreModel();
                $proxy_result_id = $ProxyFactorScoreModel->where($ResultWhere)->column('result_id');
                $ConsultOfflineFactorScoreModel = new ConsultOfflineFactorScoreModel();
                $offline_result_id = $ConsultOfflineFactorScoreModel->where($ResultWhere)->column('result_id');
                $ConsultOnlineFactorScoreModel = new ConsultOnlineFactorScoreModel();
                $online_result_id = $ConsultOnlineFactorScoreModel->where($ResultWhere)->column('result_id');
                $result_ids = array_merge($task_result_id, $self_result_id, $proxy_result_id, $offline_result_id, $online_result_id);
                $result_ids = array_unique($result_ids);
            }
            if (!empty($param['is_explain'])) {
                $TaskUserResultModel = new TaskUserResultModel();
                $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                $ConsultOnlineResultModel = new ConsultOnlineResultModel();
                $ConsultOfflineResultModel = new ConsultOfflineResultModel();
                $ResultWhere = [];
                if ($param['is_explain'] == 1) {
                    $ResultWhere[] = ['is_explain', '=', 3];
                } else {
                    $ResultWhere[] = ['is_explain', '<', 3];
                }
                $task_result_id = $TaskUserResultModel->where($ResultWhere)->column('id');
                $self_result_id = $SelfEvaluationResultModel->where($ResultWhere)->column('id');
                $proxy_result_id = $ProxyEvaluationResultModel->where($ResultWhere)->column('id');
                $online_result_id = $ConsultOnlineResultModel->where($ResultWhere)->column('id');
                $offline_result_id = $ConsultOfflineResultModel->where($ResultWhere)->column('id');
                $result_id = array_merge($task_result_id, $self_result_id, $proxy_result_id, $online_result_id, $offline_result_id);
                $result_id = array_unique($result_id);
                if (count($result_ids)) {
                    $result_ids = array_intersect($result_ids, $result_id);
                } else {
                    $result_ids = $result_id;
                }
            }
            if (!empty($param['factor_id']) || !empty($param['warning']) || !empty($param['is_explain'])) {
                if (count($result_ids)) {
                    $result_ids = array_unique($result_ids);
                    $where[] = ['channel_id', 'in', $result_ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 0];
            $FollowUserModel = new FollowUserModel();
            $ids = $FollowUserModel->where($where)->column('id');
            if (empty($ids)) {
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 干预人群高级筛选
     * @return bool|string
     */
    public function InterfereUser(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['department_id'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['uid', '=', -1];
                }
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', 'in', $param['type_id']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', 'in', $param['level_id']];
            }
            if (!empty($param['behavior_id'])) {
                $InterfereBehaviorModel = new InterfereBehaviorModel();
                $id = $InterfereBehaviorModel->where('behavior_id', 'in', $param['behavior_id'])->column('interfere_id');
                if (count($id)) {
                    $where[] = ['id', 'in', $id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['channel'])) {
                $where[] = ['channel', 'in', $param['channel']];
            }
            if (isset($param['is_up']) && strlen($param['is_up']) > 0) {
                if ($param['is_up'] == 1) {
                    $where[] = ['up_num', '>', 0];
                } else {
                    $where[] = ['up_num', '=', 0];
                }
            }
            $ResultWhere = [];
            if (!empty($param['warning'])){
                $warning_id = [];
                $RatingScaleFactorWarningModel = new RatingScaleFactorWarningModel();
                foreach ($param['warning'] as $item) {
                    if (empty($item['mark_count'])) continue;
                    $factor_warning_id = $RatingScaleFactorWarningModel->where([
                        ['rating_scale_id','=',$item['rating_scale_id']],
                        ['mark_count','in',$item['mark_count']]
                    ])->column('id');
                    if (count($factor_warning_id)) {
                        $warning_id = array_merge($warning_id,$factor_warning_id);
                    }
                }
                if (count($warning_id)){
                    $ResultWhere[] = ['warning_id','in',$warning_id];
                }else{
                    $ResultWhere[] = ['id','=',-1];
                }
            }
            if (!empty($param['factor_id'])) {
                $factor_id = [];
                foreach ($param['factor_id'] as $item) {
                    if (count($item)) {
                        $factor_id = array_merge($factor_id,$item);
                    }
                }
                if (count($factor_id)) {
                    $ResultWhere[] = ['factor_id', 'in', $factor_id];
                } else {
                    $ResultWhere[] = ['id', '=', -1];
                }
            }
            $result_ids = [];
            if (count($ResultWhere) > 0) {
                $TaskFactorScoreModel = new TaskFactorScoreModel();
                $task_result_id = $TaskFactorScoreModel->where($ResultWhere)->column('result_id');
                $SelfFactorScoreModel = new SelfFactorScoreModel();
                $self_result_id = $SelfFactorScoreModel->where($ResultWhere)->column('result_id');
                $ProxyFactorScoreModel = new ProxyFactorScoreModel();
                $proxy_result_id = $ProxyFactorScoreModel->where($ResultWhere)->column('result_id');
                $ConsultOfflineFactorScoreModel = new ConsultOfflineFactorScoreModel();
                $offline_result_id = $ConsultOfflineFactorScoreModel->where($ResultWhere)->column('result_id');
                $ConsultOnlineFactorScoreModel = new ConsultOnlineFactorScoreModel();
                $online_result_id = $ConsultOnlineFactorScoreModel->where($ResultWhere)->column('result_id');
                $result_ids = array_merge($task_result_id, $self_result_id, $proxy_result_id, $offline_result_id, $online_result_id);
                $result_ids = array_unique($result_ids);
            }
            if (!empty($param['is_explain'])) {
                $TaskUserResultModel = new TaskUserResultModel();
                $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                $ConsultOnlineResultModel = new ConsultOnlineResultModel();
                $ConsultOfflineResultModel = new ConsultOfflineResultModel();
                $ResultWhere = [];
                if ($param['is_explain'] == 1) {
                    $ResultWhere[] = ['is_explain', '=', 3];
                } else {
                    $ResultWhere[] = ['is_explain', '<', 3];
                }
                $task_result_id = $TaskUserResultModel->where($ResultWhere)->column('id');
                $self_result_id = $SelfEvaluationResultModel->where($ResultWhere)->column('id');
                $proxy_result_id = $ProxyEvaluationResultModel->where($ResultWhere)->column('id');
                $online_result_id = $ConsultOnlineResultModel->where($ResultWhere)->column('id');
                $offline_result_id = $ConsultOfflineResultModel->where($ResultWhere)->column('id');
                $result_id = array_merge($task_result_id, $self_result_id, $proxy_result_id, $online_result_id, $offline_result_id);
                $result_id = array_unique($result_id);
                if (count($result_ids)) {
                    $result_ids = array_intersect($result_ids, $result_id);
                } else {
                    $result_ids = $result_id;
                }
            }
            if (!empty($param['factor_id']) || !empty($param['warning']) || !empty($param['is_explain'])) {
                if (count($result_ids)) {
                    $result_ids = array_unique($result_ids);
                    $where[] = ['channel_id', 'in', $result_ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['move_status', '=', 1];
            $where[] = ['status', '=', 0];
            $InterfereUserModel = new InterfereUserModel();
            $ids = $InterfereUserModel->where($where)->column('id');
            if (empty($ids)) {
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 干预完成档案高级筛选
     * @return bool|string
     */
    public function InterfereUserArchives(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['status_time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['status_time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['status_time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['department_id'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['uid', '=', -1];
                }
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', 'in', $param['type_id']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', 'in', $param['level_id']];
            }
            if (!empty($param['behavior_id'])) {
                $InterfereBehaviorModel = new InterfereBehaviorModel();
                $id = $InterfereBehaviorModel->where('behavior_id', 'in', $param['behavior_id'])->column('interfere_id');
                if (count($id)) {
                    $where[] = ['id', 'in', $id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['channel'])) {
                $where[] = ['channel', 'in', $param['channel']];
            }
            if (isset($param['is_up']) && strlen($param['is_up']) > 0) {
                if ($param['is_up'] == 1) {
                    $where[] = ['up_num', '>', 0];
                } else {
                    $where[] = ['up_num', '=', 0];
                }
            }
            if (!empty($param['is_explain'])) {
                $TaskUserResultModel = new TaskUserResultModel();
                $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                $ConsultOnlineResultModel = new ConsultOnlineResultModel();
                $ConsultOfflineResultModel = new ConsultOfflineResultModel();
                $ResultWhere = [];
                if ($param['is_explain'] == 1) {
                    $ResultWhere[] = ['is_explain', '=', 3];
                } else {
                    $ResultWhere[] = ['is_explain', '<', 3];
                }
                $task_result_id = $TaskUserResultModel->where($ResultWhere)->column('id');
                $self_result_id = $SelfEvaluationResultModel->where($ResultWhere)->column('id');
                $proxy_result_id = $ProxyEvaluationResultModel->where($ResultWhere)->column('id');
                $online_result_id = $ConsultOnlineResultModel->where($ResultWhere)->column('id');
                $offline_result_id = $ConsultOfflineResultModel->where($ResultWhere)->column('id');
                $result_id = array_merge($task_result_id, $self_result_id, $proxy_result_id, $online_result_id, $offline_result_id);
                $result_id = array_unique($result_id);
                $result_ids = $result_id;
            }
            if (!empty($param['is_explain'])) {
                if (!empty($result_ids)) {
                    $result_ids = array_unique($result_ids);
                    $where[] = ['channel_id', 'in', $result_ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
            $where[] = ['status', '=', 1];
            $InterfereUserModel = new InterfereUserModel();
            $ids = $InterfereUserModel->where($where)->column('id');
            if (empty($ids)) {
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 自我测评报告高级筛选
     * @return bool|string
     */
    public function SelfEvaluationReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['department_id'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['sex'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('sex', '=', $param['sex'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['type_id'])) {
                $RatingScaleModel = new RatingScaleModel();
                $rating_scale_id = $RatingScaleModel->where('type_id', 'in', $param['type_id'])->column('id');
                if (count($rating_scale_id)) {
                    $where[] = ['rating_scale_id', 'in', $rating_scale_id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['score'])) {
                foreach ($param['score'] as $item) {
                    $ScoreData = $this->ScoreSelectorConfig($item);
                    $where[] = ['total_score', $ScoreData['calculation'], $ScoreData['value']];
                }
            }
            $FactorScoreWhere = [];
            if (!empty($param['warning'])){
                $warning_id = [];
                $RatingScaleFactorWarningModel = new RatingScaleFactorWarningModel();
                foreach ($param['warning'] as $item) {
                    if (empty($item['mark_count'])) continue;
                    $factor_warning_id = $RatingScaleFactorWarningModel->where([
                        ['rating_scale_id','=',$item['rating_scale_id']],
                        ['mark_count','in',$item['mark_count']]
                    ])->column('id');
                    if (count($factor_warning_id)) {
                        $warning_id = array_merge($warning_id,$factor_warning_id);
                    }
                }
                if (count($warning_id)){
                    $FactorScoreWhere[] = ['warning_id','in',$warning_id];
                }else{
                    $FactorScoreWhere[] = ['id','=',-1];
                }
            }
            if (!empty($param['factor_id'])) {
                $factor_id = [];
                foreach ($param['factor_id'] as $item) {
                    if (count($item)) {
                        $factor_id = array_merge($factor_id,$item);
                    }
                }
                if (count($factor_id)) {
                    $FactorScoreWhere[] = ['factor_id', 'in', $factor_id];
                } else {
                    $FactorScoreWhere[] = ['id', '=', -1];
                }
            }
            if (!empty($FactorScoreWhere)) {
                $SelfFactorScoreModel = new SelfFactorScoreModel();
                $result_id = $SelfFactorScoreModel->where($FactorScoreWhere)->column('result_id');
                if (count($result_id)) {
                    $where[] = ['id', 'in', $result_id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['answer_role'])) {
                $where[] = ['answer_role', '=', $param['answer_role']];
            }
            if (isset($param['is_explain']) && strlen($param['is_explain']) > 0) {
                $where[] = ['is_explain', '=', $param['is_explain']];
            }
            if (isset($param['is_follow']) && strlen($param['is_follow']) > 0) {
                $where[] = ['is_follow', '=', $param['is_follow']];
            }
            if (!empty($param['result_read_auth'])) {
                foreach ($param['result_read_auth'] as $item) {
                    $where[] = ['result_read_auth', 'like', '%'.$item.'%','or'];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            $where[] = ['admin_del', '=', 0];
            if (!empty($param['unit_id'])){
                $where[] = ['unit_id', '=', $param['unit_id']];
            }else{
                $where[] = ['unit_id', '=', $this->userData['unit_id']];
            }
            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
            $ids = $SelfEvaluationResultModel->where($where)->column('id');
            if (empty($ids)) {
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 他人测评报告高级筛选
     * @return bool|string
     */
    public function ProxyEvaluationReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['department_id'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['sex'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('sex', '=', $param['sex'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['proxy_uid'])) {
                $where[] = ['proxy_uid', 'in', $param['proxy_uid']];
            }
            if (!empty($param['type_id'])) {
                $RatingScaleModel = new RatingScaleModel();
                $rating_scale_id = $RatingScaleModel->where('type_id', 'in', $param['type_id'])->column('id');
                if (count($rating_scale_id)) {
                    $where[] = ['rating_scale_id', 'in', $rating_scale_id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['score'])) {
                foreach ($param['score'] as $item) {
                    $ScoreData = $this->ScoreSelectorConfig($item);
                    $where[] = ['total_score', $ScoreData['calculation'], $ScoreData['value']];
                }
            }
            $FactorScoreWhere = [];
            if (!empty($param['warning'])){
                $warning_id = [];
                $RatingScaleFactorWarningModel = new RatingScaleFactorWarningModel();
                foreach ($param['warning'] as $item) {
                    if (empty($item['mark_count'])) continue;
                    $factor_warning_id = $RatingScaleFactorWarningModel->where([
                        ['rating_scale_id','=',$item['rating_scale_id']],
                        ['mark_count','in',$item['mark_count']]
                    ])->column('id');
                    if (count($factor_warning_id)) {
                        $warning_id = array_merge($warning_id,$factor_warning_id);
                    }
                }
                if (count($warning_id)){
                    $FactorScoreWhere[] = ['warning_id','in',$warning_id];
                }else{
                    $FactorScoreWhere[] = ['id','=',-1];
                }
            }
            if (!empty($param['factor_id'])) {
                $factor_id = [];
                foreach ($param['factor_id'] as $item) {
                    if (count($item)) {
                        $factor_id = array_merge($factor_id,$item);
                    }
                }
                if (count($factor_id)) {
                    $FactorScoreWhere[] = ['factor_id', 'in', $factor_id];
                } else {
                    $FactorScoreWhere[] = ['id', '=', -1];
                }
            }
            if (!empty($FactorScoreWhere)) {
                $ProxyFactorScoreModel = new ProxyFactorScoreModel();
                $result_id = $ProxyFactorScoreModel->where($FactorScoreWhere)->column('result_id');
                if (count($result_id)) {
                    $where[] = ['id', 'in', $result_id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (isset($param['is_warning']) && strlen($param['is_warning']) > 0) {
                $where[] = ['is_warning', '=', $param['is_warning']];
            }
            if (isset($param['is_explain']) && strlen($param['is_explain']) > 0) {
                $where[] = ['is_explain', '=', $param['is_explain']];
            }
            if (isset($param['is_follow']) && strlen($param['is_follow']) > 0) {
                $where[] = ['is_follow', '=', $param['is_follow']];
            }
            if (!empty($param['result_read_auth'])) {
                foreach ($param['result_read_auth'] as $item) {
                    $where[] = ['result_read_auth', 'like', '%'.$item.'%','or'];
                }
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            $where[] = ['admin_del', '=', 0];
            if (!empty($param['unit_id'])){
                $where[] = ['unit_id', '=', $param['unit_id']];
            }else{
                $where[] = ['unit_id', '=', $this->userData['unit_id']];
            }
            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
            $ids = $ProxyEvaluationResultModel->where($where)->column('id');
            if (empty($ids)) {
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 预警报告高级筛选
     * @return bool|string
     */
    public function WarningReport(): bool|string
    {
        try {
            $param = $this->request->param();
            $ids = [];
            $WarningReportRecordModel = new WarningReportRecordModel();
            if (!empty($param['source'])) {
                foreach ($param['source'] as $item) {
                    switch ($item) {
                        case 1:
                            $where = $this->WarningReportWhere($param, 1);
                            $TaskUserResultModel = new TaskUserResultModel();
                            $result_id = $TaskUserResultModel->where($where)->column('id');
                            $id = $WarningReportRecordModel->where([
                                ['result_id', 'in', $result_id],
                                ['source','=',1]
                            ])->column('id');
                            $ids = array_merge($ids,$id);
                            break;
                        case 2:
                            $where = $this->WarningReportWhere($param, 2);
                            $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                            $result_id = $SelfEvaluationResultModel->where($where)->column('id');
                            $id = $WarningReportRecordModel->where([
                                ['result_id', 'in', $result_id],
                                ['source','=',2]
                            ])->column('id');
                            $ids = array_merge($ids,$id);
                            break;
                        case 3:
                            $where = $this->WarningReportWhere($param, 3);
                            $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                            $result_id = $ProxyEvaluationResultModel->where($where)->column('id');
                            $id = $WarningReportRecordModel->where([
                                ['result_id', 'in', $result_id],
                                ['source','=',3]
                            ])->column('id');
                            $ids = array_merge($ids,$id);
                            break;
                        default:
                            throw new \Exception('参数错误！');
                    }
                }
            }else{
                //任务
                $where = $this->WarningReportWhere($param, 1);
                $TaskUserResultModel = new TaskUserResultModel();
                $result_id = $TaskUserResultModel->where($where)->column('id');
                $id = $WarningReportRecordModel->where([
                    ['result_id', 'in', $result_id],
                    ['source','=',1]
                ])->column('id');
                $ids = array_merge($ids,$id);
                //自我
                $where = $this->WarningReportWhere($param, 2);
                $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                $result_id = $SelfEvaluationResultModel->where($where)->column('id');
                $id = $WarningReportRecordModel->where([
                    ['result_id', 'in', $result_id],
                    ['source','=',2]
                ])->column('id');
                $ids = array_merge($ids,$id);
                //他人
                $where = $this->WarningReportWhere($param, 3);
                $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                $result_id = $ProxyEvaluationResultModel->where($where)->column('id');
                $id = $WarningReportRecordModel->where([
                    ['result_id', 'in', $result_id],
                    ['source','=',3]
                ])->column('id');
                $ids = array_merge($ids,$id);
            }
            if (count($ids)){
                $ids = array_unique($ids);
            }else{
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

    /**
     * 预警报告高级筛选-条件
     */
    public function WarningReportWhere($param, $source): array
    {
        $FactorScoreModel = match ($source) {
            1 => new TaskFactorScoreModel(),
            2 => new SelfFactorScoreModel(),
            3 => new ProxyFactorScoreModel()
        };
        $where = [];
        if (!empty($param['department_id'])) {
            $UserModel = new UserModel();
            $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
            if (count($uid)) {
                $where[] = ['uid', 'in', $uid];
            } else {
                $where[] = ['id', '=', -1];
            }
        }
        if (!empty($param['sex'])) {
            $UserModel = new UserModel();
            $uid = $UserModel->where('sex', '=', $param['sex'])->column('id');
            if (count($uid)) {
                $where[] = ['uid', 'in', $uid];
            } else {
                $where[] = ['id', '=', -1];
            }
        }
        if (!empty($param['type_id'])) {
            $RatingScaleModel = new RatingScaleModel();
            $rating_scale_id = $RatingScaleModel->where('type_id', 'in', $param['type_id'])->column('id');
            if (count($rating_scale_id)) {
                $where[] = ['rating_scale_id', 'in', $rating_scale_id];
            } else {
                $where[] = ['id', '=', -1];
            }
        }
        if (!empty($param['score'])) {
            foreach ($param['score'] as $item) {
                $ScoreData = $this->ScoreSelectorConfig($item);
                $where[] = ['total_score', $ScoreData['calculation'], $ScoreData['value']];
            }
        }
        $FactorScoreWhere = [];
        if (!empty($param['warning'])){
            $warning_id = [];
            $RatingScaleFactorWarningModel = new RatingScaleFactorWarningModel();
            foreach ($param['warning'] as $item) {
                if (empty($item['mark_count'])) continue;
                $factor_warning_id = $RatingScaleFactorWarningModel->where([
                    ['rating_scale_id','=',$item['rating_scale_id']],
                    ['mark_count','in',$item['mark_count']]
                ])->column('id');
                if (count($factor_warning_id)) {
                    $warning_id = array_merge($warning_id,$factor_warning_id);
                }
            }
            if (count($warning_id)){
                $FactorScoreWhere[] = ['warning_id','in',$warning_id];
            }else{
                $FactorScoreWhere[] = ['id','=',-1];
            }
        }
        if (!empty($param['factor_id'])) {
            $factor_id = [];
            foreach ($param['factor_id'] as $item) {
                if (count($item)) {
                    $factor_id = array_merge($factor_id,$item);
                }
            }
            if (count($factor_id)) {
                $FactorScoreWhere[] = ['factor_id', 'in', $factor_id];
            } else {
                $FactorScoreWhere[] = ['id', '=', -1];
            }
        }
        if (!empty($FactorScoreWhere)) {
            $result_id = $FactorScoreModel->where($FactorScoreWhere)->column('result_id');
            if (count($result_id)) {
                $where[] = ['id', 'in', $result_id];
            } else {
                $where[] = ['id', '=', -1];
            }
        }
        if (!empty($param['factor_id'])) {
            $factor_id = [];
            foreach ($param['factor_id'] as $item) {
                if (count($item)) {
                    $factor_id = array_merge($factor_id,$item);
                }
            }
            $result_id = $FactorScoreModel->where([
                ['factor_id', 'in', $factor_id]
            ])->column('result_id');
            if (count($result_id)) {
                $where[] = ['id', 'in', $result_id];
            } else {
                $where[] = ['id', '=', -1];
            }
        }
        if (!empty($param['warning_id'])) {
            $result_id = $FactorScoreModel->where([
                ['warning_id', 'in', $param['warning_id']]
            ])->column('result_id');
            if (count($result_id)) {
                $where[] = ['id', 'in', $result_id];
            } else {
                $where[] = ['id', '=', -1];
            }
        }
        if (!empty($param['is_explain'])) {
            $where[] = ['is_explain', '=', $param['is_explain']];
        }
        if (!empty($param['answer_role'])) {
            $where[] = ['answer_role', '=', $param['answer_role']];
        }
        if (!empty($param['result_read_auth'])) {
            foreach ($param['result_read_auth'] as $item) {
                $where[] = ['result_read_auth', 'like', '%'.$item.'%','or'];
            }
        }
        if (!empty($param['start_time']) && !empty($param['end_time'])) {
            $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
        } else {
            if (!empty($param['start_time'])) {
                $where[] = ['time', '>=', $param['start_time']];
            }
            if (!empty($param['end_time'])) {
                $where[] = ['time', '<=', $param['end_time']];
            }
        }
        $where[] = ['admin_del', '=', 0];
        if (!empty($param['unit_id'])){
            $where[] = ['unit_id', '=', $param['unit_id']];
        }else{
            $where[] = ['unit_id', '=', $this->userData['unit_id']];
        }
        return $where;
    }

    /**
     * 已发布任务记录-高级筛选
     * @return bool|string
     */
    public function task(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['department_id'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
                if (count($uid)) {
                    $where[] = ['uid', 'in', $uid];
                }else{
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['type_id'])) {
                $RatingScaleModel = new RatingScaleModel();
                $rating_scale_id = $RatingScaleModel->where('type_id', 'in', $param['type_id'])->column('id');
                if (count($rating_scale_id)) {
                    $where[] = ['rating_scale_id', 'in', $rating_scale_id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['result_read_auth'])){
                $TaskUserResultModel = new TaskUserResultModel();
                $ResultWhere = [];
                foreach ($param['result_read_auth'] as $item) {
                    $ResultWhere[] = ['result_read_auth', 'like', '%'.$item.'%','or'];
                }
                $ResultWhere[] = ['unit_id','=',$this->userData['unit_id']];
                $result_id = $TaskUserResultModel->where($ResultWhere)->column('id');
                if (count($result_id)) {
                    $where[] = ['result_id', 'in', $result_id];
                }else{
                    $where[] = ['id', '=', -1];
                }
            }
            $TaskUserModel = new TaskUserModel();
            $task_id = $TaskUserModel->where($where)->column('task_id');
            $TaskWhere = [];
            if (count($task_id)){
                $TaskWhere[] = ['id', 'in', $task_id];
            }else{
                $TaskWhere[] = ['id', '=', -1];
            }
            if (!empty($param['uid'])){
                $TaskWhere[] = ['uid', 'in', $param['uid']];
            }
            if (!empty($param['status'])){
                $TaskWhere[] = ['status', '=', $param['status']];
            }
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $TaskWhere[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $TaskWhere[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $TaskWhere[] = ['time', '<=', $param['end_time']];
                }
            }
            $TaskModel = new TaskModel();
            $ids = $TaskModel->where($TaskWhere)->column('id');
            if (empty($ids)) {
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }


    /**
     * 实施干预
     * @return bool|string
     */
    public function InterfereUser2(): bool|string
    {
        try {
            $param = $this->request->param();
            $where = [];
            if (!empty($param['start_time']) && !empty($param['end_time'])) {
                $where[] = ['time', 'between', [$param['start_time'], $param['end_time']]];
            } else {
                if (!empty($param['start_time'])) {
                    $where[] = ['time', '>=', $param['start_time']];
                }
                if (!empty($param['end_time'])) {
                    $where[] = ['time', '<=', $param['end_time']];
                }
            }
            if (!empty($param['department_id'])) {
                $UserModel = new UserModel();
                $uid = $UserModel->where('department_id', 'in', $param['department_id'])->column('id');
                if (!empty($uid)) {
                    $where[] = ['uid', 'in', $uid];
                } else {
                    $where[] = ['uid', '=', -1];
                }
            }
            if (!empty($param['type_id'])) {
                $where[] = ['type_id', 'in', $param['type_id']];
            }
            if (!empty($param['level_id'])) {
                $where[] = ['level_id', 'in', $param['level_id']];
            }
            if (!empty($param['behavior_id'])) {
                $InterfereBehaviorModel = new InterfereBehaviorModel();
                $id = $InterfereBehaviorModel->where('behavior_id', 'in', $param['behavior_id'])->column('interfere_id');
                if (!empty($id)) {
                    $where[] = ['id', 'in', $id];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            if (!empty($param['channel'])) {
                $where[] = ['channel', 'in', $param['channel']];
            }
            if (isset($param['is_up']) && strlen($param['is_up']) > 0) {
                if ($param['is_up'] == 1) {
                    $where[] = ['up_num', '>', 0];
                } else {
                    $where[] = ['up_num', '=', 0];
                }
            }
            $ResultWhere = [];
            if (!empty($param['warning'])){
                $warning_id = [];
                $RatingScaleFactorWarningModel = new RatingScaleFactorWarningModel();
                foreach ($param['warning'] as $item) {
                    if (empty($item['mark_count'])) continue;
                    $factor_warning_id = $RatingScaleFactorWarningModel->where([
                        ['rating_scale_id','=',$item['rating_scale_id']],
                        ['mark_count','in',$item['mark_count']]
                    ])->column('id');
                    if (!empty($factor_warning_id)) {
                        $warning_id = array_merge($warning_id,$factor_warning_id);
                    }
                }
                if (!empty($warning_id)){
                    $ResultWhere[] = ['warning_id','in',$warning_id];
                }else{
                    $ResultWhere[] = ['id','=',-1];
                }
            }
            if (!empty($param['factor_id'])) {
                $factor_id = [];
                foreach ($param['factor_id'] as $item) {
                    if (!empty($item)) {
                        $factor_id = array_merge($factor_id,$item);
                    }
                }
                if (!empty($factor_id)) {
                    $ResultWhere[] = ['factor_id', 'in', $factor_id];
                } else {
                    $ResultWhere[] = ['id', '=', -1];
                }
            }
            $result_ids = [];
            if (!empty($ResultWhere)) {
                $TaskFactorScoreModel = new TaskFactorScoreModel();
                $task_result_id = $TaskFactorScoreModel->where($ResultWhere)->column('result_id');
                $SelfFactorScoreModel = new SelfFactorScoreModel();
                $self_result_id = $SelfFactorScoreModel->where($ResultWhere)->column('result_id');
                $ProxyFactorScoreModel = new ProxyFactorScoreModel();
                $proxy_result_id = $ProxyFactorScoreModel->where($ResultWhere)->column('result_id');
                $ConsultOfflineFactorScoreModel = new ConsultOfflineFactorScoreModel();
                $offline_result_id = $ConsultOfflineFactorScoreModel->where($ResultWhere)->column('result_id');
                $ConsultOnlineFactorScoreModel = new ConsultOnlineFactorScoreModel();
                $online_result_id = $ConsultOnlineFactorScoreModel->where($ResultWhere)->column('result_id');
                $result_ids = array_merge($task_result_id, $self_result_id, $proxy_result_id, $offline_result_id, $online_result_id);
                $result_ids = array_unique($result_ids);
            }
            if (!empty($param['is_explain'])) {
                $TaskUserResultModel = new TaskUserResultModel();
                $SelfEvaluationResultModel = new SelfEvaluationResultModel();
                $ProxyEvaluationResultModel = new ProxyEvaluationResultModel();
                $ConsultOnlineResultModel = new ConsultOnlineResultModel();
                $ConsultOfflineResultModel = new ConsultOfflineResultModel();
                $ResultWhere = [];
                if ($param['is_explain'] == 1) {
                    $ResultWhere[] = ['is_explain', '=', 3];
                } else {
                    $ResultWhere[] = ['is_explain', '<', 3];
                }
                $task_result_id = $TaskUserResultModel->where($ResultWhere)->column('id');
                $self_result_id = $SelfEvaluationResultModel->where($ResultWhere)->column('id');
                $proxy_result_id = $ProxyEvaluationResultModel->where($ResultWhere)->column('id');
                $online_result_id = $ConsultOnlineResultModel->where($ResultWhere)->column('id');
                $offline_result_id = $ConsultOfflineResultModel->where($ResultWhere)->column('id');
                $result_id = array_merge($task_result_id, $self_result_id, $proxy_result_id, $online_result_id, $offline_result_id);
                $result_id = array_unique($result_id);
                if (!empty($result_ids)) {
                    $result_ids = array_intersect($result_ids, $result_id);
                } else {
                    $result_ids = $result_id;
                }
            }
            if (!empty($param['factor_id']) || !empty($param['warning']) || !empty($param['is_explain'])) {
                if (!empty($result_ids)) {
                    $result_ids = array_unique($result_ids);
                    $where[] = ['channel_id', 'in', $result_ids];
                } else {
                    $where[] = ['id', '=', -1];
                }
            }
            $where[] = ['is_del4','=',0];
            $where[] = ['evaluate_status','=',2];
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $InterfereUserModel = new InterfereUserModel();
            $ids = $InterfereUserModel->where($where)->column('id');
            if (empty($ids)) {
                $ids = [-1];
            }
            return success($ids);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);
        }
    }

}